
local mt = {}
mt.hard = 0


--变动间隔
mt.time1 = 6
--Boss间隔
mt.time2 = 360
--失败时间
mt.time3 = 90
mt.boss_index = 0
mt.boss_max = 5

--波数
mt.level = 0

--最大波数
mt.max_level = 1800/mt.time1

 
local boss_list = {
    '[领主]恶魔巫师',
    '[领主]恶魔巨兽',
    '[领主]毁灭守卫',
}
--出怪
function mt:wait_boss(time)
    self.boss_index = self.boss_index + 1
    if self.exit_timer2 then self.exit_timer2:remove() end

    self.exit_timer2 = ac.wait(self.time2*1000,function(t)
        local boss = ('首领%s'):format(self.boss_index)
        if self.boss_index<self.boss_max then
            ac.each_player(function(player)
                local unit = player:create_enemy(boss,player.atk2)
                unit:add_skill('Boss施法')
                unit:event '单位-死亡'(function()
                    player:notify('玩家-创建Boss宝箱',player,unit:get_point())
                end)
            end)
            self:wait_boss()
        else
            if ac.base_unit then
                ac.base_unit:remove()
                ac.base_unit = nil
            end
            ac.game:notify('游戏-关闭挑战')
            ac.game:notify('游戏-关闭资源')
            ac.each_player(function(player)
                player:notify('玩家-传送',player,player.point1)
            end)
            self:clear()
            
            local hp_rate = {100,180,240,280}
            local count = ac.player.get_online_count()
            local rate = hp_rate[count] or hp_rate[#hp_rate]
            local unit = ac.player[12]:create_unit(boss,ac.base_point)
            unit:add('生命加成',rate - 100)
            unit:add_buff 'Boss出场'{}
            unit:add_skill('Boss施法')
            unit:event '单位-死亡'(function()
                ac.game:notify('游戏-游戏胜利',self)
            end)
            self.exit_timer2 = ac.wait(self.time3*1000,function(t)
                ac.game:notify('游戏-游戏失败',self)
            end)
            ac.player.self:set_camera(unit:get_point(),0.1)
            ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'游戏失败')
        end
    end)
    self.title = ('正常模式 | 难%s'):format(self.hard)
    if self.boss_index==self.boss_max then
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'决战：')
    else
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,('首领%s：'):format(self.boss_index))
    end
end

--出怪
local params = load_excel('数据投放.xlsx','挑战等级')
function mt:loop_unit()
    if self.exit_timer0 then self.exit_timer0:remove() end
    if self.exit_timer1 then self.exit_timer1:remove() end
    self.elite_name = '巨型骷髅'

    self.exit_timer0 = ac.loop(self.time1*1000,function(t)
        self.level = self.level + 1
        local lv = math.floor(self.level/10)
        self.atk_range = params[lv] and params[lv]['远程小怪距离']
        ac.each_player(function(player)
            player:set_data('小怪额外移速',math.floor(self.level * self.time1/(6*60)) * 45)
        end)
        if lv%6==3 then
            self.weight = {
                {'骷髅战士',3},
                {'骷髅法师',1},
            }
        else
            self.weight = {
                {'骷髅战士',3},
            }
        end
        if self.level==self.max_level then
            t:remove()
            ac.game:notify('游戏-停止出怪',self)
        else
            ac.game:notify('游戏-新的一波',self,self.level)
        end
    end)
    self.exit_timer0:on_timer()

    --精英怪数量
    local time = 30

    self.exit_timer1 = ac.loop(time*1000,function()
        ac.each_player(function(player)
            local unit = player:create_enemy(self.elite_name)
            unit:set('移动速度',math.min(math.max(self.level,100),250)+20)
            unit['木材'] = 20
        end)
    end)
    
    self.exit_timer3 = ac.loop(70*1000,function()
        ac.each_player(function(player)
            local count = player:get_data('装备宝箱数量',0)
            local index = player:get_data('阶段',0)
            local point = nil
            if index==0 then
                point = player.zb_rect:random()
            end
            local unit = player:create_enemy('装备宝箱',point)
            unit:wait(280*1000,function()
                unit.owner_player = nil
                unit:kill()
            end)
            --unit:add_buff '挑战限时'{time = 280}
            -- if count<4 then
            --     player:set_data('装备宝箱数量',count + 1)
            --     local unit = player:create_enemy('装备宝箱')
            --     unit:event '单位-死亡'(function()
            --         local count = player:get_data('装备宝箱数量',0)
            --         player:set_data('装备宝箱数量',count - 1)
            --     end)
            -- end
        end)
    end)
end


function mt:clear()
    if self.exit_timer0 then self.exit_timer0:remove() end
    if self.exit_timer1 then self.exit_timer1:remove() end
    if self.exit_timer2 then self.exit_timer2:remove() end
    if self.exit_timer3 then self.exit_timer3:remove() end
    --for u in pairs(self.units) do
    --    u:remove()
    --end
    ac.each_player(function(player)
        player:clear_enemy()
    end)
    
    for u in ac.selector('unit')
        : range(ac.point(0,0),20000)
        : can_god()
        : enemy(ac.player[1])
        : loop()
    do
        u:remove()
    end

end

--[游戏时间]
function mt:get_time()
    return ac.clock() - self.start_time
end

function mt:init(time)
    local time = time or 10
    self.units = {}
    self.start_time = ac.clock()
    ac.game:notify('游戏-开始出怪')
    self.exit_timer1 = ac.wait(time*1000,function()
        self:loop_unit()
        self:wait_boss()
    end)
    
end


--获取刷怪点
function ac.player.__index:getAtkPoint()
    local index = self:get_data('阶段',0)
    if index==0 then
        return self.atk1:random()
    elseif index==1 then
        return self.atk2:random()
    end
    return self.atk2:random()
end


ac.game:event '单位-初始化'(function(_,unit)
    if unit.name=='骷髅法师' then
        unit:set('攻击距离',ac.enemy.atk_range)
    end
end)

ac.game:event '游戏-设置难度'(function(_,hard)
    local data = setmetatable({}, {__index = mt})
    ac.enemy = data
    data.hard = hard
    data:init(2)
end)


local test_data = {
    {name = '关闭出怪',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
    {name = '强化怪物',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
    {name = '刷Boss',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
    {name = '刷宝箱',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
}

ac.wait(10,function()
    ac.game:notify('测试-注册数据','测试流程',test_data,function(_,player,name)
        if name=='关闭出怪' then
            ac.enemy:clear()
            if player.sb_timer then
                player.sb_timer:remove()
            end
        elseif name=='强化怪物' then
            if ac.enemy.exit_timer0 then
                ac.enemy.level = ac.enemy.level + 9
                ac.enemy.exit_timer0:on_timer()
            end
        elseif name=='刷Boss' then
            ac.enemy.exit_timer2:on_timer()
        elseif name=='刷宝箱' then
            ac.enemy.exit_timer3:on_timer()
        end
    end)


end)


return mt